package com.jetgege.smartbox.AddBox;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;

import com.jetgege.smartbox.Base.BaseActivity;
import com.jetgege.smartbox.BoxSetting.InputFingerprintActivity;
import com.jetgege.smartbox.R;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

public class AuthenticationActivity extends BaseActivity {


    @BindView(R.id.et_DevicePWDInput)
    EditText etDevicePWDInput;
    @BindView(R.id.ib_DevicePWDClear)
    ImageButton ibDevicePWDClear;
    @BindView(R.id.authentication_toolbar)
    Toolbar authenticationToolbar;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_authentication;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initValue();
    }

    private void initValue() {

        authenticationToolbar.setTitle("密码验证");
        setSupportActionBar(authenticationToolbar);
        authenticationToolbar.setNavigationIcon(R.drawable.bar_back_btn);
        authenticationToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
            }
        });
        //调出键盘
        Timer timer = new Timer(true);
        timer.schedule(new TimerTask() {
            public void run() {
                InputMethodManager inputManager = (InputMethodManager) etDevicePWDInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(etDevicePWDInput, 0);
            }
        }, 500);

        etDevicePWDInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length()>0)ibDevicePWDClear.setVisibility(View.VISIBLE);
                else ibDevicePWDClear.setVisibility(View.GONE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.authentication, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        finish();
        Intent intent =new Intent(AuthenticationActivity.this,InputFingerprintActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
        return true;
    }

    @OnClick(R.id.ib_DevicePWDClear)
    public void onViewClicked() {
        etDevicePWDInput.setText("");
    }


}
